package app;

import game.MainApp;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import server.Server;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LauncherApp extends JFrame {

	private JPanel contentPane;
	private JTextField txtClientNick;
	private JTextField txtClientIP;
	private JTextField txtServerIP;
	
	private JLabel lblStartEnServer,lblStartEnKlient,lblNick,lblIP,lblServerIp;
	private JButton btnStartServer,btnConnect, btnStopServer;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LauncherApp frame = new LauncherApp();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LauncherApp() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 396, 206);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblStartEnServer = new JLabel("Start en server");
		lblStartEnServer.setBounds(10, 11, 144, 14);
		contentPane.add(lblStartEnServer);
		
		txtServerIP = new JTextField();
		txtServerIP.setBounds(30, 126, 124, 20);
		contentPane.add(txtServerIP);
		txtServerIP.setColumns(10);
		
		btnStartServer = new JButton("Start server");
		btnStartServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SwingWorker<String, String> bck = 
				new SwingWorker<String, String>() {

					@Override
					protected String doInBackground() throws Exception {
						Server.getInstance().run();
						return "";
					}
				};
				bck.execute();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				txtServerIP.setText(Server.getInstance().getSocket().getInetAddress().getHostAddress() + "");
				btnStartServer.setEnabled(false);
				btnStopServer.setEnabled(true);
			}
		});
		btnStartServer.setBounds(30, 40, 124, 23);
		contentPane.add(btnStartServer);
		
		lblStartEnKlient = new JLabel("Start en klient");
		lblStartEnKlient.setBounds(231, 11, 80, 14);
		contentPane.add(lblStartEnKlient);
		
		lblNick = new JLabel("Nick:");
		lblNick.setBounds(241, 40, 46, 14);
		contentPane.add(lblNick);
		
		txtClientNick = new JTextField();
		txtClientNick.setBounds(273, 37, 86, 20);
		contentPane.add(txtClientNick);
		txtClientNick.setColumns(10);
		
		lblIP = new JLabel("IP:");
		lblIP.setBounds(241, 78, 46, 14);
		contentPane.add(lblIP);
		
		txtClientIP = new JTextField("192.168.1.");
		txtClientIP.setBounds(273, 75, 86, 20);
		contentPane.add(txtClientIP);
		txtClientIP.setColumns(10);
		
		btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new BtnConnectActionListener());
		btnConnect.setBounds(231, 106, 131, 23);
		contentPane.add(btnConnect);
		
		btnStopServer = new JButton("Stop server");
		btnStopServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Server.getInstance().kill();
				txtServerIP.setText("");
				btnStartServer.setEnabled(true);
				btnStopServer.setEnabled(false);
			}
		});
		btnStopServer.setBounds(30, 74, 124, 23);
		contentPane.add(btnStopServer);
		
		lblServerIp = new JLabel("Server IP: ");
		lblServerIp.setBounds(10, 107, 63, 14);
		contentPane.add(lblServerIp);
		
		
	}
	private class BtnConnectActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			SwingWorker<String,String> bck = new SwingWorker<String,String>() {

				@Override
				protected String doInBackground() throws Exception {
					try {
						MainApp.run(txtClientIP.getText(), txtClientNick.getText());
					} catch (Exception e1) {				
						e1.printStackTrace();
					}
					return "";
				}				
			};
			bck.execute();			
		}
	}
}
